package com.huawei.simplepiano.audioplayer;

import com.huawei.simplepiano.ResourceTable;
import com.huawei.simplepiano.utils.ConstantUtils;
import com.huawei.simplepiano.utils.LogUtil;
import ohos.app.Context;
import ohos.media.audio.AudioManager;
import ohos.media.audio.SoundPlayer;

import java.util.HashMap;

public class FourPianoAudio {
    //声音播放器
    private final SoundPlayer soundFourPlayer;
    //声音播放器集合
    private final HashMap<Integer, Integer> soundPlayerFourMap;
    //声音播放器参数
    private final SoundPlayer.SoundPlayerParameters soundPlayerParameters;
    int[] fourAudio = {
            //小字一组
            ResourceTable.Media_b_c_1, //c1
            ResourceTable.Media_b_d_1, //d1
            ResourceTable.Media_b_e_1, //e1
            ResourceTable.Media_b_f_1, //f1
            ResourceTable.Media_b_g_1, //g1
            ResourceTable.Media_b_a_1, //a1
            ResourceTable.Media_b_b_1, //b1
            //小字一组m
            ResourceTable.Media_h_c_1, //c1m
            ResourceTable.Media_h_d_1, //d1m
            ResourceTable.Media_h_f_1, //f1m
            ResourceTable.Media_h_g_1, //g1m
            ResourceTable.Media_h_a_1, //a1m
    };

    public FourPianoAudio(Context context) {
        soundFourPlayer = new SoundPlayer(AudioManager.AudioVolumeType.STREAM_MUSIC.getValue());
        soundPlayerParameters = new SoundPlayer.SoundPlayerParameters();
        SoundPlayer.AudioVolumes audioVolumes = new SoundPlayer.AudioVolumes();
        audioVolumes.setCentralVolume(100.0f);
        soundPlayerParameters.setVolumes(audioVolumes);
        soundPlayerParameters.setLoop(SoundPlayer.SoundPlayerParameters.NO_LOOP);
        soundPlayerParameters.setSpeed(SoundPlayer.SoundPlayerParameters.NORMAL_SPEED_RATE);
        soundPlayerParameters.setPriority(SoundPlayer.SoundPlayerParameters.PLAY_IMMEDIATELY_PRIORITY);
        soundPlayerFourMap = new HashMap<>();
        for (int i = 0; i < fourAudio.length; i++) {
            soundPlayerFourMap.put(i, soundFourPlayer.createSound(context, fourAudio[i]));
        }
    }

    public void soundFourPlay(int number) {
        soundFourPlayer.play(soundPlayerFourMap.get(number), soundPlayerParameters);
    }
}
